package com.jy.adscm.operation.cg.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgBomSupplier;
import com.jy.adscm.operation.cg.bean.CgSupplier;
import com.jy.adscm.operation.cg.bean.QueryCgSuppiler;
import com.jy.adscm.operation.cg.mapper.mysql.CgBomSupplierMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgSupplierMapper;
import com.jy.adscm.operation.cg.service.CgSupplierService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 供应商基础信息表 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-07-07
 */
@Service
public class CgSupplierServiceImpl extends ServiceImpl<CgSupplierMapper, CgSupplier> implements CgSupplierService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @Autowired
    CgBomSupplierMapper cgBomSupplierMapper;

    @Override
    public Page<CgSupplier> selectCgSupplier(Page<CgSupplier> page, QueryCgSuppiler queryCgSuppiler) throws AdscmSystemException {
        Wrapper<CgSupplier> wrapper =new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgSuppiler.getName(),null)){
            wrapper.like("name",queryCgSuppiler.getName());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgSuppiler.getType(),null)){
            wrapper.eq("type",queryCgSuppiler.getType());
        }
        wrapper.orderBy("createTime",false);
        page = super.selectPage(page, wrapper);
        List<CgSupplier> cgSuppliers = page.getRecords();
        for (CgSupplier cgSupplier : cgSuppliers) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(cgSupplier.getCreator());
                cgSupplier.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier.getPaymentMethod(),null)){
                JyCode  jyCode =new JyCode();
                jyCode.setCodeType(6);
                jyCode.setCode(cgSupplier.getPaymentMethod().toString());
                jyCode= jyCodeMapper.selectOne(jyCode);
                cgSupplier.setPaymentMethodName(jyCode.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier.getTransporttationMethod(),null)){
                JyCode  jyCode =new JyCode();
                jyCode.setCodeType(7);
                jyCode.setCode(cgSupplier.getTransporttationMethod().toString());
                jyCode= jyCodeMapper.selectOne(jyCode);
                cgSupplier.setTransporttationMethodName(jyCode.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier.getMakeInvoiceType(),null)){
                JyCode  jyCode =new JyCode();
                jyCode.setCodeType(5);
                jyCode.setCode(cgSupplier.getMakeInvoiceType().toString());
                jyCode= jyCodeMapper.selectOne(jyCode);
                cgSupplier.setMakeInvoiceTypeName(jyCode.getName());
            }

        }
        page.setTotal(super.selectCount(wrapper));
        return page;
    }

    @Override
    public List<CgSupplier> selectByBid(Integer bid) {
        List<CgSupplier> suppliers =new ArrayList<>();
        Wrapper<CgBomSupplier> cgBomSupplierWrapper=new EntityWrapper<>();
        cgBomSupplierWrapper.eq("bid",bid);
        List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(cgBomSupplierWrapper);
        for (CgBomSupplier cgBomSupplier : cgBomSuppliers) {
            CgSupplier cgSupplier = super.selectById(cgBomSupplier.getSid());
            cgSupplier.setCgBomSupplier(cgBomSupplier);
            suppliers.add(cgSupplier);
        }
        return suppliers;
    }
}
